package com.vod.yn.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.List;

/**
 * Created by xiuqun.hxq on 17/4/21.
 */
public class ResultModel<T> {

    private T data; // 返回实体
    private int totalPage = 0; // 分页总数
    private long total = 0; // 总记录为数,默认为1,一般情况下,当<T> 为集合类型时才有意义
    private boolean success = true; // 当且仅当接口发生异常时为false,其它为true
    private int rtnCode = 0; // 返回错误码
    private String rtnMsg = ""; // 错误消息

    /**
     * 产生一个ResultModel对象,泛化
     *
     * @param <T>
     * @return
     */
    public static <T> ResultModel<T> newResultModel() {
        return new ResultModel<T>();
    }

    public static <T> ResultModel<T> error_900() {
        ResultModel<T> result = newResultModel();
        result.setRtnCode(900);
        result.setRtnMsg("not login");
        result.setSuccess(true);
        return result;
    }

    public static <T> ResultModel<T> ok_200(){
        ResultModel<T> result = newResultModel();
        result.setRtnCode(200);
        result.setSuccess(true);
        return result;
    }

    public static <T> ResultModel<T> error_505(List msgList) {
        ResultModel<T> result = newResultModel();
        result.setRtnCode(505);
        result.setRtnMsg("服务端数据异常,reason:" + msgList);
        result.setSuccess(false);
        return result;
    }
    public static <T> ResultModel<T> error_400(String msg) {
        ResultModel<T> result = newResultModel();
        result.setRtnCode(400);
        result.setRtnMsg("请求参数异常,reason:" + msg);
        result.setSuccess(false);
        return result;
    }

    public static <T> ResultModel<T> error_501(String msg) {
        ResultModel<T> result = newResultModel();
        result.setRtnCode(500);
        result.setRtnMsg(msg);
        result.setSuccess(false);
        return result;
    }

    public static <T> ResultModel<T> error_500(String msg) {
        ResultModel<T> result = newResultModel();
        result.setRtnCode(500);
        result.setRtnMsg(msg);
        result.setSuccess(false);
        return result;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getRtnCode() {
        return rtnCode;
    }

    public void setRtnCode(int rtnCode) {
        this.rtnCode = rtnCode;
    }

    public String getRtnMsg() {
        return rtnMsg;
    }

    public void setRtnMsg(String rtnMsg) {
        this.rtnMsg = rtnMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTotal() {
        if (this.data == null) {
            return 0;
        }
        // 如果设置了total值,则直接返回设置的值
        if (this.total > 0) {
            return this.total;
        }
        // 如果是集合类型,则返回集合的元素数量
        if (this.data instanceof List) {
            List list = JSON.parseObject(JSON.toJSONString(this.data), new TypeReference<List>() {
            });
            return list.size();
        }
        return 1;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("ResultModel{");
        sb.append("data=").append(data);
        sb.append(", totalPage=").append(totalPage);
        sb.append(", total=").append(total);
        sb.append(", success=").append(success);
        sb.append(", rtnCode=").append(rtnCode);
        sb.append(", rtnMsg='").append(rtnMsg).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
